console.log(Object.is(NaN, NaN));

let obj = { id: 2 }
var id = 1
function print() {
    console.log(this.id)
}

Function.prototype.myBind = function (context) {
    if (typeof this !== 'function') {
        throw new Error('不是一个函数')
    }
    fn = this
    // 获取参数
    var args = [...arguments].slice(1)
    return function Fn() {
        return fn.apply(this instanceof Fn ? this : context, args.concat(...arguments))
    }
}

print()
let temp = print.myBind(obj)
new temp()

// let b = new Promise((_, reject) => { reject(1) })
// console.log(b);



async function print1() {
    console.log('start');
    await print2()
    console.log('print1');
}

async function print2() {
    console.log('print2');
}

console.log('script');
print1()
console.log('end');


let promise1 = new Promise((resole, reject) => {
    setTimeout(() => {
        resole('没有超时，请求发送')
    }, 6000)
})

function timeout(delay) {
    return new Promise((resole, reject) => {
        setTimeout(() => {
            resole('请求超时')
        }, delay)
    })
}

Promise.race([promise1, timeout(5000)]).then(res => console.log(res))

/**
 * 实现rand随机中奖函数
 * n为抽奖人员，w越大中奖概率越大
 **/ 
let peoples = [
    { n: 'p1', w: 860 },
    { n: 'p2', w: 200 },
    { n: 'p3', w: 100 },
]
let rand = function (p) {
    let sum = peoples.reduce((pre, cur) => {
        return pre + cur.w
    }, 0)
    peoples.forEach(item => {
        item.w = item.w / sum * 100
    })
    let arr = []
    for(let item of peoples) {
        arr.push(...new Array(Math.floor(item.w)).fill(item.n))
    }
    let len = arr.length

    if(arr[Math.floor(Math.random().toFixed(2)*len)] === p) {
        console.log('恭喜你中奖');
    }
    else {
        console.log('很遗憾没有中奖');
    }
}
rand('p1')

// 判断数组最深层,层数
let arr = [1,3,3,[4,[6],7,[5,6,7,43,[23,4]]],[1,2]]

const getArrDepth = (arr, deth) => {
    arr.forEach(item => {
        if(Array.isArray(item)) getArrDepth(item, deth+1)
    })
    return deth
}
console.log(getArrDepth(arr, 1));


var countDigitOne = function(n) {
    if(n < 10) return 1
    let res = 0
    for(let i = 1; i <= n; i++) {
        if(String(i).includes('1')) {
            res += getOne(String(i))
        }
    }
    function getOne(s) {
        let sum = 0
        for(let item of s) {
            if(item === '1') sum += 1
        }
        return sum
    }
    return res
    };
    console.log(countDigitOne(824883294))